package com.campus_information_portal.service.impl;

import com.campus_information_portal.dao.*;
import com.campus_information_portal.entity.*;
import com.campus_information_portal.service.MarkService;
import com.campus_information_portal.util.Message;
import com.campus_information_portal.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 20155884 王晓阳
 * @className : MarkImpl
 * @Description
 * @create 2018/04/19 15:37
 **/
@Service
public class MarkImpl implements MarkService {
    @Autowired
    private MarkDao markdao;
    @Autowired
    private TermDao termDao;
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private WarningDao warningDao;
    @Autowired
    private MarkCheckDao markCheckdao;

    @Override
    public Message findStuIdByMark(String courseName) {
        List<MarkCondition> markList = markdao.findStuIdByMark(courseName);
        System.out.println("markList" + markList.size());
        for (MarkCondition m : markList) {
            System.out.println("学生id：" + m.getStuInfoId());
        }
        if (markList.size() != 0) {
            return Message.success().add("markList", markList);
        } else {
            return Message.fail().add("0901", "查询失败");
        }
    }
    /**
     * @param stu_info_id
     * @Description 学生查询成绩(学生登录成功后)，根据学生id查找
     * @author 20155884 王晓阳
     * @methodName findAllMarkByStuId
     * @returnType Msg
     * @create 2018/4/26
     **/
   /* @Override
    public Msg findAllMarkByStuId(int stu_info_id) {
        List<Mark> markList = markdao.findAllMarkByStuId(stu_info_id);
        if (markList.size() != 0) {
            return Msg.addData(100, "成功", markList);
        }
        return Msg.fail();
    }*/

    /**
     * @param
     * @Description 根据学生id和学期查找所有成绩
     * @author 20155884 王晓阳
     * @methodName findAllMarkByStuIdAndTermId
     * @returnType Msg
     * @create 2018/5/3 10:14
     **/
   /* @Override
    public Msg findAllMarkByStuIdAndTermId(int stu_info_id, int term_id) {
        List<Mark> markList = markdao.findAllMarkByStuIdAndTermId(stu_info_id, term_id);
        if (markList.size() != 0) {
            return Msg.addData(100, "成功", markList);
        }
        return Msg.fail();
    }*/

    /**
     * @param term
     * @Description 老师根据学期查找所有学生成绩
     * @author 20155884 王晓阳
     * @methodName findMarkByTerm
     * @returnType Msg
     * @create 2018/5/3 10:36
     **/
   /* @Override
    public Msg findMarkByTerm(Term term) {
        List<Mark> markList = markdao.findMarkByTerm(term);
        if (markList.size() != 0) {
            return Msg.addData(100, "成功", markList);
        }
        return Msg.fail();
    }*/

    /**
     * @author 20155884 王晓阳
     * @methodName :selectMark
     * @Description 动态查询成绩。
     * @create 2018/05/07 19:35
     **/
    @Override
    public Message selectMark(MarkCondition markCondition) {
        List<MarkCondition> markList = new ArrayList<MarkCondition>();
        markList = markdao.selectMark(markCondition);
        System.out.println("大小：" + markList.size());
        for (MarkCondition m : markList) {
            System.out.println("成绩：" + m.getMark() + "学生id：" + m.getStuInfoId() + "   学分：" + m.getCourseCredithour() + "学期id=" + m.getTermId() + "  姓名：" + m.getStuName());
        }
       /* Map<String,Object> map=new HashMap<String,Object>();
        map.put("studentList",studentList);
        map.put("markList",markList);
        System.out.println("map大小"+map.size());
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue().toString());
        }*/
        // message.add("studentList",studentList);
        //   System.out.println("st"+studentList.size());
        if (markList.size() != 0) {
            //zkl加
            for (MarkCondition m : markList) {
                int markCheckState = markCheckdao.queryStateByMarkId(m.getMarkId());
                if (markCheckState == 0) {
                    m.setApplyMarkCheckState(0);//可申请
                } else {
                    m.setApplyMarkCheckState(1);//不可申请
                }
            }
            return Message.success().add("markList", markList);

        }
        return Message.fail().add("0901", "查询失败");
    }

    /**
     * @param markCondition
     * @Description 根据学生id查找学生总学分和上面动态查询成绩共用一个方法
     * @author 20155884 王晓阳
     * @methodName selectourseCredithour
     * @returnType Msg
     * @create 2018/5/22 11:03
     **/
    @Override
    public Message selectCourseCredithour(MarkCondition markCondition) {
        List<MarkCondition> markList = markdao.selectMark(markCondition);
        System.out.println("--markList--" + markList.size());
        int TotalcourseCredithour = 0;
        int TotalcourseCredithour1 = 0;
        int TotalcourseCredithour2 = 0;
        for (MarkCondition m : markList) {
            if (m.getMark() < 60) {
                TotalcourseCredithour1 = 0;
                //TotalcourseCredithour1 = TotalcourseCredithour + m.getCourseCredithour();
                System.out.println("---不及格---总学分：" + TotalcourseCredithour);
            } else {
                TotalcourseCredithour2 = TotalcourseCredithour2 + m.getCourseCredithour();
                System.out.println("---及格---总学分：" + TotalcourseCredithour);
            }
            TotalcourseCredithour = TotalcourseCredithour1 + TotalcourseCredithour2;
            System.out.println("成绩：" + m.getMark() + "学生id：" + m.getStuInfoId() + "   学分：" + m.getCourseCredithour());
        }
        System.out.println("总学分：" + TotalcourseCredithour);
        if (markList.size() != 0) {
            return Message.success().add("markList", markList);
        }
        return Message.fail().add("0901", "查询失败");
    }

    /**
     * @param markCondition
     * @Description 查询学生的总学分
     * @author 20155884 王晓阳
     * @methodName findStuCredithour
     * @returnType Msg
     * @create 2018/5/10 11:48
     **/
    @Override
    public Message findStuCredithour1(MarkCondition markCondition) {
        MarkCondition markCondition1= new MarkCondition();
        Warning selectWarning=new Warning();
        List<MarkCondition> markConditionList=new ArrayList<MarkCondition>();

        //获取所有学生
        List<MarkCondition> studentList = studentDao.queryAllStudent();
        System.out.println("stulist,size:" + studentList.size());
        //遍历学生，查找每个学生的学分
        for (MarkCondition s : studentList) {
            System.out.println("stuid" + s.getStuInfoId());
            markCondition.setStuInfoId(s.getStuInfoId());
            //调用查询每个学生实际得到的学分
            markCondition1=markdao.findStuCredithour1(markCondition);
            //获取应得总学分
            int actualCredithour = markdao.findTotalCourseCredithour(markCondition.getTermId());
            markCondition1.setActualCredithour(actualCredithour);
            System.out.println("actualCredithour=" + actualCredithour);

            //查询学业警示表中num的个数
            Warning warning=new Warning();
            warning.setStuInfoId(s.getStuInfoId());
            warning.setTermId(markCondition.getTermId());
            selectWarning=warningDao.selectWarningByIds(warning);
            //判断查询出的warningNum是否为空，如果不为空就将取得的值赋给markCondition1,如果为空就赋0
            if (selectWarning!=null){
                markCondition1.setWarningNum(selectWarning.getWarningNum());
            }else{
                markCondition1.setWarningNum(0);
            }
            System.out.println("sssss---"+markCondition1.toString());
            //判断学生所修学分是否够总学分的一半，如果小于实际的，就将要获得学业警示的学生存入list
            if (markCondition1.getStuCourseCresithour()*2<actualCredithour){
                markConditionList.add(markCondition1);
            }
        }
        System.out.println("mclist大小："+markConditionList.size()+"---"+markConditionList.toString());
        return Message.success().add("markConditionList",markConditionList);
    }

    /**
     * @param term_id
     * @Description 在courseterm表中按学期查询本学期所有课程的总学分
     * @author 20155884 王晓阳
     * @methodName findTotalCourseCredithour
     * @returnType Msg
     * @create 2018/5/22 11:15
     **/
    @Override
    public int findTotalCourseCredithour(int term_id) {
        Integer totalCourseCredithour = markdao.findTotalCourseCredithour(term_id);
        System.out.println("本学期所有学分总计：" + totalCourseCredithour);
       /* if (totalCourseCredithour != null) {
            return Msg.addData(100, "成功", totalCourseCredithour);
        }
        return Msg.fail();*/
        return totalCourseCredithour;
    }

    /**
     * @param
     * @Description
     * @author 20155884 王晓阳
     * @methodName addMark
     * @returnType void
     * @create 2018/5/3 9:33
     **/
    @Override
    public Message addMark(MarkCondition markCondition) {
        int flag = markdao.addMark(markCondition);
        if (flag == 1) {
            return Message.success().add("100", "成功");
        }
        return Message.fail().add("0901", "查询失败");
    }

    /**
     * @param markCondition
     * @Description 通过成绩id修改成绩
     * @author 20155884 王晓阳
     * @methodName updateMarkByMarkId
     * @returnType void
     * @create 2018/5/7 16:15
     **/
    @Override
    public Message updateMarkByMarkId(MarkCondition markCondition) {
        int flag = markdao.updateMarkByMarkId(markCondition);
        if (flag == 1) {
            return Message.success().add("100", "成功");
        }
        return Message.fail().add("0901", "查询失败");
    }

    /**
     * @param markCondition
     * @Description 查询不及格成绩的个数
     * @author 20155884 王晓阳
     * @methodName findFailGradeCount
     * @returnType Msg
     * @create 2018/5/10 11:48
     **/
    @Override
    public Message findFailGradeCount(MarkCondition markCondition) {
        Integer count = markdao.findFailGradeCount(markCondition);
        System.out.println("不及格个数：" + count);
        if (count != null) {

            return Message.success().add("100", "成功");
        }
        return Message.fail().add("0901", "查询失败");
    }

    /**
     * @param markCondition
     * @Description 查询不及格成绩按学科查，按学号查，按学期查
     * @author 20155884 王晓阳
     * @methodName selectFallGrade
     * @returnType List
     * @create 2018/5/10 11:48
     **/
    @Override
    public Message selectFallGrade(MarkCondition markCondition) {
        List<MarkCondition> markList = markdao.selectFallGrade(markCondition);
        for (MarkCondition m : markList) {
            System.out.println("成绩：" + m.getMark() + "   学生id：" + m.getStuInfoId());
        }
        if (markList.size() != 0) {
            //zkl加
            for (MarkCondition m : markList) {
                int markCheckState = markCheckdao.queryStateByMarkId(m.getMarkId());
                if (markCheckState == 0) {
                    m.setApplyMarkCheckState(0);//可申请
                } else {
                    m.setApplyMarkCheckState(1);//不可申请
                }
            }
            return Message.success().add("markList", markList);
        }
        return Message.fail().add("0901", "查询失败");
    }

    /**
     * @param
     * @Description 按学期和课程id查询在mark表不在学生表中的学生信息，即查询没有录成绩的学生个数
     * @author 20155884 王晓阳
     * @methodName
     * @returnType
     * @create 2018/5/27 17:29
     **/
    //select * from student left join mark on student.stu_info_id=mark.stu_info_id where mark.stu_info_id is null
    public Msg queryNoScoreStudent(MarkCondition markCondition) {
        List<Student> studentList = new ArrayList<Student>();
        //查找学生id
        List<MarkCondition> markList = markdao.queryNoScoreStudent(markCondition);
        System.out.println("-个数dd-" + markList.size());
        for (MarkCondition m : markList) {
            System.out.println("-markImpl-queryNoScoreStudent-学号：" + m.getStuInfoId());
            //通过学生id查找学生信息
            Student student = studentDao.queryStudentByStuId(m.getStuInfoId());
            System.out.println("-markImpl-queryNoScoreStudent-姓名：" + student.getStuName() + "  学号：" + student.getStuNum());
            studentList.add(student);
        }
        System.out.println("学生list大小:" + studentList.size());
        if (studentList.size() == 0) {
            //studentList.size()==0说明已录完state=0，没录完，state=1录完
            System.out.println("状态置1");
            markCondition.setState(1);
            markdao.updateCoursetermState(markCondition);

        } else {
            markCondition.setState(0);
            System.out.println("没录完");
            markdao.updateCoursetermState(markCondition);
        }

        if (studentList != null) {
            return Msg.addData(100, "成功", studentList.size());
        }
        return Msg.fail();
    }

    /**
     * @param
     * @Description 根据学期id，courseid coursename 查询课程
     * @author 20155884 王晓阳
     * @methodName
     * @returnType
     * @create 2018/5/28 8:00
     **/
    public Message findCourseTerm(MarkCondition markCondition) {
        System.out.println("%%%%%%%%%%"+markCondition.toString());
        List<MarkCondition> markList = markdao.findCourseTerm(markCondition);

        Message message = new Message();
        Object noScorestudent=queryNoScoreStudent(markCondition).getData();
        System.out.println("NiNOONNONON"+noScorestudent);

        if (markList.size() != 0) {
            message.add("markList", markList);
            return message;
        }
        return message.fail().add("0901", "查询失败");
    }

    /**
     * @param
     * @Description 更新courseterm状态
     * @author 20155884 王晓阳
     * @methodName
     * @returnType
     * @create 2018/5/28 17:44
     **/
    @Override
    public Message updateCoursetermState(MarkCondition markCondition) {
        System.out.println("uuuuuuuuu--------------");
        Integer flag = markdao.updateCoursetermState(markCondition);
        System.out.println("uuuuuuuuu");
        if (flag != null) {
            return Message.success().add("100", "成功");
        }
        return Message.fail().add("0901", "查询失败");
    }

    /**
     * @param
     * @Description 查询各分数段的学生人数
     * @author 20155884 王晓阳
     * @methodName
     * @returnType
     * @create 2018/6/1 19:21
     **/
    public Message selectMarkCount(MarkCount markCount){
       MarkCount count= markdao.selectMarkCount(markCount);
        System.out.println("-selectMarkCount-"+count.toString());
        return Message.success().add("markCount",count);
    }
}
